package com.sgb.kjwl.viewmodel.enterprise;

import android.app.Application;
import android.text.TextUtils;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sgb.kjwl.api.NewAPIManager;
import com.sgb.kjwl.model.entity.enterprise.ServiceNodeListEntity;
import com.sgb.kjwl.model.entity.enterprise.ServiceProjectClassEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.swgk.core.base.model.entity.BaseEntity;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * 服务流程节点model
 */
public class ServiceNodeListModel extends AndroidViewModel {

    public MutableLiveData<List<ServiceNodeListEntity>> serviceNodeList = new MutableLiveData<>();
    public MutableLiveData<Integer> ShowLoading = new MutableLiveData<>();
    public MutableLiveData<String> toast = new MutableLiveData<>();
    public List<ServiceProjectClassEntity> serviceProList = new ArrayList<>();
    public List<Map> maps = new ArrayList<>();

    private int limit = 20;
    public int pageSize = 1;

    public ServiceNodeListModel(@NonNull @NotNull Application application) {
        super(application);
    }

    /**
     * 服务流程节点 列表
     *
     * @param isLodMore
     * @param serviceId
     */
    public void getServiceNodeList(boolean isLodMore, String serviceId) {
        pageSize = isLodMore ? pageSize+1 : 1;
        ShowLoading.postValue(1);
        NewAPIManager.getInstance().getEnterPrisenewAPI().getServiceNodeList(limit, pageSize, serviceId).enqueue(new Callback<BaseEntity<ListResponse<List<ServiceNodeListEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<ServiceNodeListEntity>>>> call, Response<BaseEntity<ListResponse<List<ServiceNodeListEntity>>>> response) {
                ShowLoading.postValue(2);
                if (response.body() == null || response.body().getData() == null)
                    return;
                if (response.body().getState().equals("ok")) {
                    serviceNodeList.postValue(response.body().getData().getList());
                } else {
                    toast.postValue(response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<ServiceNodeListEntity>>>> call, Throwable t) {
                ShowLoading.postValue(2);
            }
        });
    }


    public List<ServiceProjectClassEntity> serviceProToBean(String json) {
        serviceProList = new Gson().fromJson(json, new TypeToken<List<ServiceProjectClassEntity>>() {
        }.getType());
        return serviceProList;
    }

    public List<Map> servicePriceToBean(String json) {
        maps = new Gson().fromJson(json, new TypeToken<List<Map>>() {
        }.getType());
        return maps;
    }
}


